package com.cilico.single.components;

import java.util.Calendar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.cilico.single.R;
import com.cilico.single.utils.Utils;
import com.cilico.single.view.BaseView;
import com.cilico.single.view.FormLayout;
import com.cilico.single.wheel.ScreenInfo;
import com.cilico.single.wheel.WheelMain;

public class TimePicker extends TextView implements Component {

	BaseView bv_;
	int width_;
	int height_;
	public String attrValue_ = null;
	WheelMain wheelMain;
	TextPaint paint_;
	int fontSize_ = 14;
	float scale;
	/** 对齐方式 */
	String align_;
	
	/** 控件名称 */
	public String attrName_;

	public TimePicker(BaseView bv, String value, String align, String width, String height) {
		super(bv);
		bv_ = bv;
		attrValue_ = value;
		align_ = align;
		paint_ = new TextPaint();
		paint_.setTextSize(Utils.getScaledValue(fontSize_));
		scale = Utils.SCALEDATE_W * Utils.DM_DENSITY;
		if (null != width) {
			width_ = Utils.getScaledValue(Integer.parseInt(width));
		} else {
			width_ = Utils.getScaledValue(100);
		}
		if (null != height) {
			height_ = Utils.getScaledValue(Integer.parseInt(height));
		} else {
			height_ = Utils.getScaledValue(50);
		}
		LinearLayout.LayoutParams pm = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		setLayoutParams(pm);
		setText(value);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		try {
			// 圆角矩形圆角弧度
			int radian = 10;
			paint_.setColor(Color.parseColor("#aad4fd"));
			canvas.drawRoundRect(new RectF(0, 0, width_, height_), radian, radian, paint_);
			paint_.setColor(Color.BLACK);

			// 默认左对齐
			int startX = 10;
			if (null != align_) {
				int w = Utils.getScaledValue(paint_.measureText(attrValue_));
				if ("right".equalsIgnoreCase(align_)) {
					startX = width_ - w + 10;
				} else if ("middle".equalsIgnoreCase(align_)) {
					startX = (width_ - w) / 2;
				}
			}
			// 画label文字
			canvas.drawText(attrValue_, startX, (height_ + fontSize_ * scale) / 2 - 4, paint_);
		} catch (Exception e) {
			invalidate();
			Utils.printException(e);
		}
	}

	public boolean onTouchEvent(MotionEvent me) {
		int action = me.getAction();
		switch (action) {
		case MotionEvent.ACTION_UP:
			LayoutInflater inflater = LayoutInflater.from(bv_);
			final View timepickerview = inflater.inflate(R.layout.timepicker, null);
			ScreenInfo screenInfo = new ScreenInfo(bv_);
			wheelMain = new WheelMain(timepickerview);
			wheelMain.screenheight = screenInfo.getHeight();
			attrValue_ = getText().toString();
			Calendar calendar = Calendar.getInstance();
			int hour = calendar.get(Calendar.HOUR_OF_DAY);
			int minute = calendar.get(Calendar.MINUTE);
			wheelMain.initTimePicker(hour, minute);
			new AlertDialog.Builder(bv_)
					.setTitle("选择时间")
					.setView(timepickerview)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									attrValue_ = wheelMain.getTime();
									setText(attrValue_);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
								}
							}).show();
			break;
		}
		return true;
	}

	@Override
	public String getContentText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setContentText(String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void shrinkWidth() {
		// TODO Auto-generated method stub

	}

	@Override
	public void cleanText() {
		// TODO Auto-generated method stub

	}

	@Override
	public void mould() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setCmpWidth(int width) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getCmpWidth() {
		// TODO Auto-generated method stub
		return width_;
	}

	@Override
	public void setCmpHeidht(int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getCmpHeight() {
		// TODO Auto-generated method stub
		return height_;
	}

	@Override
	public void setCssStyle(CssStyle style) {
		// TODO Auto-generated method stub

	}

	@Override
	public View getCmpView() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void setInTable(boolean inTable) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isInTable() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public CssStyle getCssStyle() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void releaseResource(View view) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setForm(FormLayout formLayout) {
		// TODO Auto-generated method stub

	}

	@Override
	public FormLayout getForm() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEncrypt(String encrypt) {
		// TODO Auto-generated method stub

	}

}
